package com.hongli.blog.controller;

import com.hongli.blog.config.Result;
import com.hongli.blog.entity.BugDao;
import com.hongli.blog.mapper.Bugmapper;
import com.hongli.blog.scheduledtask.InitializationController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * @Author ZERO
 * @Date 2021/11/17 17:38
 **/
@RestController
@RequestMapping(value = "/BugController")
@CrossOrigin
public class BugsController {

    @Autowired
    private Bugmapper bugmapper;

    private InitializationController INITI = new InitializationController();

    /** 返回所以bug内容
      * @author ZERO
      * @date 2021/11/17 17:43
    **/
    @GetMapping(value = "/getBugContent")
    public Result getBugContent(){
        List<BugDao> listBug = bugmapper.SelectAllBug();
        if(listBug.size() <= 0){
            return Result.fail("2000");
        }
        ArrayList<Object> list = new ArrayList<>();
        for(BugDao b : listBug){
            HashMap<String, Object> map = new HashMap<>();
            String d = b.getBugsolvetime() == null?"":INITI.Times(b.getBugsolvetime());
            map.put("bugid",b.getBugid());
            map.put("buggrade",b.getBuggrade());
            map.put("bugcontent",b.getBugcontent());
            map.put("bugsolve",b.getBugsolve());
            map.put("bugcreatetime",INITI.Times(b.getBugcreatetime()));
            map.put("bugsolvetime",d);
            list.add(map);
        }
        return Result.fail("2000",list);
    }

    /** 添加bug
      * @author ZERO
      * @date 2021/11/18 9:14
    **/
    @PostMapping(value = "/InsertBUG")
    public Result insertBUG(@RequestBody Map<String, Object> map){
        String D = map.get("D").toString().trim(),
                C = map.get("C").toString().trim(),
                S = map.get("S").toString().trim();
        int I = Integer.parseInt(map.get("I").toString().trim());
        if(D.length() <= 0 && C.length() <= 0){
            return Result.fail("1000");
        }
        if(I > 0){
            if(S.length() <= 0){
                return Result.fail("1000");
            }
            BugDao bug = new BugDao(I,D,C,S,new Date(),INITI.Processing_Time());
            bugmapper.UpdateBugContent(bug);
        }else{
            if(D.length() <= 0 && C.length() <= 0){
                return Result.fail("1000");
            }
            bugmapper.InsertBug(D,C,INITI.Processing_Time());
        }
        return Result.fail("2000");
    }

    /** 回填bug
      * @author ZERO
      * @date 2021/11/20 14:05
    **/
    @PostMapping(value = "/updataBUGdata")
    public Result updataBUGdata(@RequestBody Map<String, Object> map){
        BugDao bugDao = bugmapper.SelectBug(Integer.parseInt(map.get("I").toString().trim()));
        if(bugDao == null) return Result.fail("1000");
        HashMap<String, Object> maps = new HashMap<>();
        maps.put("buggrade",bugDao.getBuggrade());
        maps.put("bugcontent",bugDao.getBugcontent());
        maps.put("bugsolve",bugDao.getBugsolve());
        return Result.fail("2000",maps);
    }

    /**
     * 删除
     * @param map
     * @return
     */
    @PostMapping(value = "/deleteBUG")
    public Result deleteBUG(@RequestBody Map<String, Object> map){
        bugmapper.DeleteBUG(Integer.parseInt(map.get("I").toString().trim()));
        return Result.fail("2000");
    }
}
